package com.sunyur.tailor.channel.service.middle.feishu.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.dal.org.entity.FSBackDO;
import com.sunyur.tailor.channel.dal.org.mapper.FSBackMapper;
import com.sunyur.tailor.channel.service.inner.workflow.param.WorkFlowSyncTaskParam;
import com.sunyur.tailor.channel.service.inner.workflow.service.SuryurWorkFlowService;
import com.sunyur.tailor.channel.service.middle.feishu.FSApprovalService;
import com.sunyur.tailor.channel.service.middle.feishu.param.ApprovalByInstanceCodeDTO;
import com.sunyur.tailor.channel.service.outer.keep.service.KeepToFeishuService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @description: 同步审批进度
 */


@Service("fsApprovalService")
@Slf4j
public class FSApprovalServiceImpl implements FSApprovalService {


    @Autowired
    private SuryurWorkFlowService suryurWorkFlowService;

    @Autowired
    private FSBackMapper fsBackMapper;


    @Autowired
    private KeepToFeishuService keepToFeishuService;

    @Value("${feishu.getApprovalByInstanceCode}")
    private String getApprovalByInstanceCode;


    @Value("${feishu.getUserNameByUserId}")
    private String getUserNameByUserId;



    @SneakyThrows
    @Override
    public ChannelResult syncTask(WorkFlowSyncTaskParam param) {

        return suryurWorkFlowService.workflowTaskComplete(param);
    }

    @Override
    public FSBackDO getFSBackDOByInstanceCode(String instanceCode) {
        return fsBackMapper.selectOne(new QueryWrapper<FSBackDO>().eq("instance_code",instanceCode));
    }

    @Override
    public ChannelResult getApprovalByInstanceCode(String instanceCode) {

        String post = getApprovalByInstanceCode+instanceCode;

        log.info("post url {}",post);
        String response = HttpUtil.createGet(post).header("Authorization", keepToFeishuService.getAuthorization()).execute().body();

        log.info("response: {}",response);
        ApprovalByInstanceCodeDTO approval = JSON.parseObject(response,ApprovalByInstanceCodeDTO.class);
        if(approval.getCode()!=0)
        {

            return ChannelResultUtil.error("50000","getApprovalByInstanceCode 飞书接口响应失败");
        }
        else {
            return ChannelResultUtil.success(approval);
        }

    }

    @Override
    public ChannelResult getApprovalNameByUserId(String userId) {

        String post = getUserNameByUserId+userId+"?department_id_type=open_department_id&user_id_type=user_id";
        log.info("post url: {}",post);
        String response = HttpUtil.createGet(post).header("Authorization", keepToFeishuService.getAuthorization()).execute().body();

        log.info("response: {}",response);


        JSONObject jsonObject = JSON.parseObject(response);
        if (jsonObject.getInteger("code")!=0)
        {
            log.error("getApprovalNameByUserId 飞书接口响应失败");

            return ChannelResultUtil.error("50000","getApprovalNameByUserId 飞书接口响应失败");
        }

        String username = jsonObject.getJSONObject("data").getJSONObject("user").getString("name");
        return ChannelResultUtil.success(username);
    }
}
